package com.proj.hot.activity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.adapter.OrderListAdapter;
import com.proj.hot.entity.PayOrderBean;
import com.proj.hot.utils.DebugUtil;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 订购记录
 */
public class OrderRecordActivity extends BaseActivity {

    private TextView dgjl;

    private ListView listView;

    private List<PayOrderBean> listData;

    private OrderListAdapter orderListAdapter;

    private String accountid = "";

    private List<JSONObject> jsList = new ArrayList<JSONObject>();
    private JSONArray jsonArray = new JSONArray();
    private JSONObject jsonObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderrecord);
        listView = (ListView) this.findViewById(R.id.order_record_list);
        dgjl = (TextView) this.findViewById(R.id.dgjl);
        if(GlobalParams.screenHeight == 1080){
            dgjl.setTextSize(25);

        }
        dgjl.setText("<  "+"订购记录");
        getOrderData();


//        listData = new ArrayList<>();

//        for (int i = 0; i < 20; i++) {
//            PayOrderBean payOrderBean = new PayOrderBean();
//            payOrderBean.setOrder_name("美国队长");
//            payOrderBean.setOrder_surplus("23小时20分");
//            payOrderBean.setOrder_price("5.0元");
//            payOrderBean.setOrder_paytag("支付宝");
//            payOrderBean.setOrder_buytime("2017-04-14 10:30");
//            payOrderBean.setOrder_validtime("48小时");
//
//            listData.add(payOrderBean);
//        }
    }

    private void getOrderData() {

        try {
            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("accountid", GlobalParams.user_id);
            DebugUtil.i("OrderRecordActivity","accountid="+GlobalParams.user_id);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "account/getOrders");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("OrderRecordActivity", "订单列表返回数据=" + s);
                        JSONObject jData = new JSONObject(s);
                        jsonObject = jData.getJSONObject("respbody");
                        jsonArray = jsonObject.getJSONArray("data");
                        jsList.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            jsList.add(jsonArray.getJSONObject(i));
                        }

                        DebugUtil.i("OrderRecordActivity", "订单个数=" + jsList.size());
                        setview();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
            DebugUtil.i("orderRecord", "获取订单请求异常！");
        }


    }

    private void setview() {
        orderListAdapter = new OrderListAdapter(this, jsList);
        listView.setAdapter(orderListAdapter);
    }


}
